<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 Netty ByteBuf 内存管理机制</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            padding: 1.5rem;
            overflow-x: auto;
            position: relative;
            margin: 1.5rem 0;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 10px;
            right: 15px;
            background: #007acc;
            color: white;
            padding: 2px 10px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: 500;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
            margin: 2rem 0 1.5rem 0;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 5px;
            height: 30px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 3px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 1rem 1.5rem;
            border-radius: 10px;
            margin: 1.5rem 0;
            box-shadow: 0 10px 20px rgba(245, 87, 108, 0.3);
        }
        
        .flow-step {
            background: #f8f9fa;
            border-left: 4px solid #667eea;
            padding: 1rem 1.5rem;
            margin: 0.5rem 0;
            border-radius: 0 8px 8px 0;
            transition: all 0.3s ease;
        }
        
        .flow-step:hover {
            background: #e9ecef;
            transform: translateX(5px);
        }
        
        .mermaid {
            margin: 2rem 0;
            text-align: center;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fade-in {
            animation: fadeInUp 0.8s ease-out;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6 relative">
        <div class="max-w-6xl mx-auto text-center relative z-10">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-fade-in">
                <i class="fas fa-memory mr-4"></i>
                深入理解 Netty ByteBuf
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90 animate-fade-in" style="animation-delay: 0.2s">
                探索高性能网络编程的内存管理艺术
            </p>
            <div class="flex justify-center space-x-6 animate-fade-in" style="animation-delay: 0.4s">
                <div class="bg-white bg-opacity-20 backdrop-filter backdrop-blur-lg rounded-lg px-6 py-3">
                    <i class="fas fa-tachometer-alt mr-2"></i>
                    高性能内存池
                </div>
                <div class="bg-white bg-opacity-20 backdrop-filter backdrop-blur-lg rounded-lg px-6 py-3">
                    <i class="fas fa-recycle mr-2"></i>
                    引用计数机制
                </div>
                <div class="bg-white bg-opacity-20 backdrop-filter backdrop-blur-lg rounded-lg px-6 py-3">
                    <i class="fas fa-bolt mr-2"></i>
                    零拷贝优化
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-5xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card rounded-2xl p-8 mb-8 animate-fade-in">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>Netty 中，<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ByteBuf</code> 是用于数据存储和缓冲的核心组件，它在内存管理中扮演着至关重要的角色。<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ByteBuf</code> 提供了一种高效的内存访问和管理方式，支持可扩展的内存池和自动内存回收机制，极大地提升了网络数据处理的性能。
            </p>
        </div>

        <!-- ByteBuf Overview -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold text-gray-800">
                <i class="fas fa-cube mr-3 text-purple-600"></i>
                1. ByteBuf 的基本概述
            </h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ByteBuf</code> 是 Netty 中用于处理字节数据的核心类。它用于存储数据、读取和写入字节流，提供了与传统 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ByteBuffer</code> 类似的功能，但在性能和灵活性上有所优化。<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ByteBuf</code> 通过内存池化的方式管理内存，减少了频繁的内存分配和垃圾回收。
            </p>
            
            <div class="code-block">
                <pre><code>public interface ByteBuf extends ReferenceCounted, Comparable&lt;ByteBuf&gt; {
    // 返回 ByteBuf 的容量
    int capacity();
    
    // 返回 ByteBuf 当前可读字节数
    int readableBytes();
    
    // 读取一个字节
    byte readByte();
    
    // 写入一个字节
    ByteBuf writeByte(int value);
    
    // 获取 ByteBuf 的底层数据
    byte[] array();
}</code></pre>
            </div>

            <div class="highlight-box">
                <h4 class="text-xl font-bold mb-3">
                    <i class="fas fa-cogs mr-2"></i>执行流程
                </h4>
                <div class="space-y-2">
                    <div class="flow-step">
                        <strong>1.</strong> <code class="bg-white bg-opacity-30 px-2 py-1 rounded">ByteBuf</code> 是一个抽象接口，不同的实现类（如 <code class="bg-white bg-opacity-30 px-2 py-1 rounded">PooledByteBuf</code>、<code class="bg-white bg-opacity-30 px-2 py-1 rounded">UnpooledByteBuf</code>）提供了具体的内存管理方式。
                    </div>
                    <div class="flow-step">
                        <strong>2.</strong> 使用 <code class="bg-white bg-opacity-30 px-2 py-1 rounded">capacity()</code> 方法获取 <code class="bg-white bg-opacity-30 px-2 py-1 rounded">ByteBuf</code> 的容量，<code class="bg-white bg-opacity-30 px-2 py-1 rounded">readableBytes()</code> 方法返回当前可读取的字节数。
                    </div>
                </div>
            </div>
        </div>

        <!-- Memory Management Strategy -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold text-gray-800">
                <i class="fas fa-database mr-3 text-purple-600"></i>
                2. ByteBuf 的内存管理策略
            </h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                Netty 中的 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ByteBuf</code> 使用内存池化机制（<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">PooledByteBufAllocator</code>）来管理内存，这意味着内存块的分配和释放不是每次都直接操作堆内存，而是通过内存池来进行复用。这种方式有效减少了内存分配的开销，降低了 GC 压力。
            </p>

            <div class="code-block">
                <pre><code>public class PooledByteBufAllocator implements ByteBufAllocator {
    private final PoolThreadCache threadCache;
    private final PoolArena&lt;byte[]&gt; heapArena;
    private final PoolArena&lt;ByteBuffer&gt; directArena;

    // 从内存池中分配一个字节数组
    public ByteBuf allocate(PooledByteBufAllocator parent) {
        PoolThreadCache cache = threadCache.get();
        ByteBuf buf = heapArena.allocate(cache);
        if (buf != null) {
            return buf;
        }
        // 如果堆内存不足，切换到直接内存池
        return directArena.allocate(cache);
    }
}</code></pre>
            </div>

            <!-- Memory Architecture Diagram -->
            <div class="mermaid">
                <div class="mermaid">
graph TD
    A[ByteBuf 内存管理架构] --> B[PooledByteBufAllocator]
    B --> C[线程缓存 ThreadCache]
    B --> D[堆内存池 HeapArena]
    B --> E[直接内存池 DirectArena]
    
    C --> F[快速分配]
    D --> G[JVM 堆内存]
    E --> H[堆外内存]
    
    F --> I[减少竞争]
    G --> J[GC 管理]
    H --> K[零拷贝]
    
    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
    style C fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
    style D fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
    style E fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        